﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Simple.Common.Core.IO
{
    public static class FileHelper
    {
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="filePath"></param>
        public static void DeleteIfExists(string filePath)
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
        }

        /// <summary>
        /// 返回文件后缀名
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string GetExtension(string filePath)
        {
            return Path.GetExtension(filePath);
        }

        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="message"></param>
        public static void WriteFile(string filePath, string message)
        {
            if (string.IsNullOrEmpty(message)) return;
            using (StreamWriter sw = new StreamWriter(filePath))
            {
                sw.Write(message);
            }
        }

        /// <summary>
        /// 追加内容到文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="message"></param>
        public static void AppendTextToFile(string filePath, string message)
        {
            if (string.IsNullOrEmpty(message)) return;
            using (StreamWriter sw = File.AppendText(filePath))
            {
                sw.Write(message);
            }
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string ReadFile(string filePath)
        {
            if (!File.Exists(filePath))
            {
                return string.Empty;
            }

            using (StreamReader sr = new StreamReader(filePath))
            {
                return sr.ReadToEnd();
            }
        }
    }
}
